package com.wlhse.service.impl;

import com.wlhse.dao.LeaderCheckItemDao;
import com.wlhse.entity.LeaderCheckItemPojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.LeaderCheckItemService;
import com.wlhse.util.SortCodeUtil;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LeaderCheckItemServiceImpl implements LeaderCheckItemService {

    @Resource
    private LeaderCheckItemDao leaderCheckItemDao;

    @Resource
    private TreeUtil treeUtil;

    @Resource
    private SortCodeUtil sortCodeUtil;

    private static final String status = "启用";

    private static final int step = 4;

    @Override
    public String queryTree() {
        return NR.r(treeUtil.getLeaderChechItemTree(leaderCheckItemDao.queryLeaderCheckItem()));
    }

    @Override
    public String update(String code) {
        if (leaderCheckItemDao.updateLeaderCheckItem(code) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    public String add(LeaderCheckItemPojo leaderCheckItemPojo) {
        leaderCheckItemPojo.setStatus(status);
        String code = leaderCheckItemPojo.getCheckItemCode();
        String maxCode = leaderCheckItemDao.getMaxCode(code, step);
        if (StringUtils.isBlank(maxCode)) {
            checkDefaultCode();
            if (StringUtils.isBlank(code))
                leaderCheckItemPojo.setCheckItemCode("A001");
            else {
                checkDefaultCode1(code);
                leaderCheckItemPojo.setCheckItemCode(code + "0001");
            }
        } else
            leaderCheckItemPojo.setCheckItemCode(sortCodeUtil.next(maxCode));
        if (leaderCheckItemDao.addLeaderCheckItem(leaderCheckItemPojo) <= 0)
            throw new WLHSException("新增失败");
        return NR.r();
    }

    private void checkDefaultCode() {
        StringBuilder code = new StringBuilder("A000");
        LeaderCheckItemPojo pojo = new LeaderCheckItemPojo();
        pojo.setStatus(status);
        pojo.setCheckItemName("其它");
        if (leaderCheckItemDao.codeCount(code.toString()) == 0) {
            pojo.setCheckItemCode(code.toString());
            leaderCheckItemDao.addLeaderCheckItem(pojo);
        }
        for (int i = 0; i < step; i++)
            code.append("0");
        if (leaderCheckItemDao.codeCount(code.toString()) == 0) {
            pojo.setCheckItemCode(code.toString());
            leaderCheckItemDao.addLeaderCheckItem(pojo);
        }
    }

    private void checkDefaultCode1(String code) {
        StringBuilder tmp = new StringBuilder(code);
        for (int i = 0; i < step; i++)
            tmp.append("0");
        if (leaderCheckItemDao.codeCount(tmp.toString()) == 0) {
            LeaderCheckItemPojo pojo = new LeaderCheckItemPojo();
            pojo.setStatus(status);
            pojo.setCheckItemName("其它");
            pojo.setCheckItemCode(tmp.toString());
            leaderCheckItemDao.addLeaderCheckItem(pojo);
        }
    }
}
